package panda.query.util;

import java.util.List;

import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;
import panda.query.struct.NullConstant;

public class OrderByComparator implements Comparator {

	List<Attribute> attrs;
	List<Boolean> order;
	
	/**
	 * 
	 * @param attrs On which attributes to compare
	 */
	public OrderByComparator(List<Attribute> attrs, List<Boolean> order) {
		this.attrs = attrs;
		this.order = order;
	}
	
	/**
	 * Compare the tuple refered to by the two scanners
	 * @param a
	 * @param b
	 * @return
	 */
	public int compare(Scanner a, Scanner b) {
		int temp;
		
		for (int i = 0; i < attrs.size(); i++) {
			Constant ca = a.getValue(attrs.get(i));
			Constant cb = b.getValue(attrs.get(i));
			
			if ((temp = ca.compareTo(cb)) != 0)
				return order.get(i) ? temp : -temp;
		}
		
		return 0;
	}

}
